package com.android.personal.finance;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.android.personal.data.FinanceDataAdaptater;
import com.android.personal.data.FinanceDataTags;
import com.android.personal.edit.OperationsEditActivity;

public class OperationsActivity extends ListActivity {
	
	private Cursor operationsCursor;
	
	private static final int EDIT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;
	
    private static final int OPERATION_CREATE = 0;
    private static final int OPERATION_EDIT = 1;
    
	public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.operations_layout);
        
        fillData();
        
        registerForContextMenu(getListView());
    }
	
	/**
	 * Options Menu Overrides.
	 * 
	 */

	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.operations_options_menu, menu);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.new_operation:
			createOperation();
			return true;
		}
		return false;
	}

	/*************************************************/
	
	/*************************************************/

	/**
	 * Context Options Menu Overrides.
	 * 
	 */

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, EDIT_ID, 0, R.string.mn_edit);
		menu.add(0, DELETE_ID, 0, R.string.mn_delete);
	}

	public boolean onContextItemSelected(MenuItem item) {

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();

		switch (item.getItemId()) {
		case EDIT_ID:
			return true;
		case DELETE_ID:
			deleteOperation(info.position);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	/*************************************************/
	
	/**
	 * Intent Results Overrides.
	 * 
	 */
	
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		
        super.onActivityResult(requestCode, resultCode, intent);
        
        if (resultCode == RESULT_CANCELED)
        	return ;
        
        Bundle extras = intent.getExtras();
        
        switch(requestCode) {
        case OPERATION_CREATE:
            break;
        case OPERATION_EDIT:
            break;
        }
        
        //fillData();
        
    }
	
	/*************************************************/
	
	
	private void createOperation() {

		Intent i = new Intent(this, OperationsEditActivity.class);
		startActivityForResult(i, OPERATION_CREATE);
	}
	
	private void deleteOperation(int position) {

		Cursor c = operationsCursor;
		c.moveToPosition(position);

		FinanceDataAdaptater.deleteOperation(c.getLong(c
				.getColumnIndexOrThrow(FinanceDataTags.OPERATION_KEY)));
		
		fillData();
	}
	
	private void fillData() {
	
		operationsCursor = FinanceDataAdaptater.fetchAllOperations();
		startManagingCursor(operationsCursor);

		String[] from = new String[] { 
				FinanceDataTags.OPERATION_DATE, 
				FinanceDataTags.ACCOUNT_NAME,
				FinanceDataTags.OPERATION_CATEGORY,
				FinanceDataTags.OPERATION_VALUE };
		int[] to = new int[] { 
				R.id.operation_date, 
				R.id.operation_account,
				R.id.operation_category, 
				R.id.operation_value};

		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.operation_row, operationsCursor, from, to);
		setListAdapter(notes);
	}

}
